#include <iostream>
#include<string>
#include<fstream>
#include<sstream>
#include <iomanip>
#include<algorithm>
#include<vector>
#include<map>
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
using std::vector;
using std::map;
using std::ofstream;
using std::istringstream;
using std::cerr;
struct Record
{
	string _word;
	int _frequency;
};

class Dictionary
{
public:
    //优化第一步：
    Dictionary(int capa){
        //vector事先给空间
        _dict.reserve(capa);
    }
    //......
    void read(const string &filename);
    void store(const string &filename);
    
private:
    vector<Record> _dict;
};
//
bool operator<(const Record&l1,const Record&L2){
    return l1._word<L2._word;
}
//读数据
void Dictionary::read(const string& filename){
    map<string,int>map_;
    ifstream ifs(filename);
    if(!ifs){
        cerr<<"ifstream open file failed!"<<endl;
        return ;
    }
    //read 兼容c的用法 要用char*存 提前要设置char的大小
    string line;
    int f=0;
    while(!ifs.eof()){
        //优化2：不是一个单词一个单词从磁盘读取
        //而是一行一行 ；不使用read因为 要提前设置char*
        //默认用换行符 作为分隔符
        getline(ifs,line);
       // cout<<line<<"________________________________________________________<<"<<endl;
        //转化成字符串流
        istringstream iss(line);
        string word;
        while(iss>>word){
            string wordT;
            for(auto ch:word){
                if(isalpha(ch)){
                    //erease 使用!!!!!!!!!!!!!!!
                    ch = tolower(ch);
                    wordT +=ch;
                }
            }
            word =wordT;    
      //      cout<<word<<endl;
        
    
        /*if(!word.empty())
        {
            map_[word]++;
        }*/
            
            for(auto & it:_dict){
        //        cout<<"_dict"<<word<<endl;
            
                if(word==it._word){
                
                    it._frequency++;
                    f=1;
                    break;
                }
            }
            if(f==1){
                f=0;
                continue;
            }
            Record r_temp;
            r_temp._word = word;
            r_temp._frequency = 1;
            _dict.push_back(r_temp);
          //  cout<<"_dict "<<word<<endl;
        }
    }
    //转化成vector
    /*for(auto & m:map_){
        Record recode_;
        recode_._word = m.first;
        recode_._frequency = m.second;
        
        _dict.push_back(recode_);
    }*/
    sort(_dict.begin(),_dict.end());
    ifs.close();
}

//存放 
void Dictionary::store(const string &filename){
    ofstream ofs(filename);
    if(!ofs){
        cerr<<"error_ofs open file failed"<<endl;
        return ;
    }
    for(const auto& dict:_dict){
        ofs<<"word:"<<std::left<<std::setw(20)<<dict._word<<"const:"<<dict._frequency<<endl;
    }

    ofs.close();
}

void test0(){
    ifstream ifs;
    ifs.open("圣经--KJV.cpp");
    string word;
    ifs.close();
}
int main()
{
    string filename = "test.txt";
    Dictionary dic(10000);
    dic.read(filename);
    string file_name = "result.txt";
    dic.store(file_name);

    return 0;
}

